import Keys from '@/constant/key'
import storage from '@/libs/storage'

// 用户 token
export const getToken = () => storage.get(Keys.token)
export const setToken = (token: string) => storage.set(Keys.token, token)
export const removeToken = () => storage.remove(Keys.token)

// 字体
export const getSize = () => storage.get(Keys.size) || 'small'
export const setSize = (size: string) => storage.set(Keys.size, size)
export const removeSize = () => storage.remove(Keys.size)

// 国际化语言
export const getLanguage = () => storage.get(Keys.language) || 'zh'
export const setLanguage = (language: string) =>
  storage.set(Keys.language, language)
export const removeLanguage = () => storage.remove(Keys.language)

// 路由
export const getRoutes = () => {
  const routes = storage.get(Keys.routes)
  if (routes === null) {
    return []
  }
  return JSON.parse(routes)
}
export const setRoutes = (routes: Array<[]>) =>
  storage.set(Keys.routes, JSON.stringify(routes))
export const removeRoutes = () => storage.remove(Keys.routes)

// 左侧菜单栏折叠
export const getCollapse = () => {
  const collapse = storage.get(Keys.collapse)
  if (collapse === null) {
    return false
  }
  return JSON.parse(collapse)
}
export const setCollapse = (collapse: boolean) =>
  storage.set(Keys.collapse, JSON.stringify(collapse))
export const removeCollapse = () => storage.remove(Keys.collapse)

// 清空数据
export const clear = () => storage.clear()
